/*
 * ============================================================================
 *
 *       Filename:  netdev.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月10日 10时18分23秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

/* netdevice */
#include <sys/ioctl.h>
#include <net/if.h>
/* sockaddr_in */
#include <netinet/in.h>

#include "common.h"

void getmac(char *nic, unsigned char *mac)
{
	int sockfd = safe_socket(AF_INET, SOCK_DGRAM, 0);

	struct ifreq req;
	strncpy(&req.ifr_name[0], nic, IFNAMSIZ);
	ioctl(sockfd, SIOCGIFHWADDR, &req);
	memcpy(mac, req.ifr_hwaddr.sa_data, ETH_ALEN);
	close(sockfd);
}

void getaddr(char *nic, int type, struct sockaddr_in *addr)
{
	int sockfd = safe_socket(AF_INET, SOCK_DGRAM, 0);

	struct ifreq req;
	strncpy(&req.ifr_name[0], nic, IFNAMSIZ);
	ioctl(sockfd, type, &req);
	memcpy(addr, &req.ifr_addr, sizeof(struct sockaddr_in));
	close(sockfd);
}

